/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:44
 */

package com.lenovo.mso2o.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ListView;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Projection;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.lenovo.framework.base.ActionBarFragment;
import com.lenovo.framework.base.BaseListAdapter;
import com.lenovo.framework.biz.ContextManager;
import com.lenovo.framework.conf.FConstants;
import com.lenovo.framework.entity.Cluster;
import com.lenovo.framework.entity.Res;
import com.lenovo.framework.rest.NetworkObserver;
import com.lenovo.framework.service.LocationEventsOnSubscribe;
import com.lenovo.framework.util.DLog;
import com.lenovo.framework.util.ViewHelper;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.conf.BizErrorCode;
import com.lenovo.mso2o.conf.ErrorCode;
import com.lenovo.mso2o.conf.MSConstants;
import com.lenovo.mso2o.entity.Order;
import com.lenovo.mso2o.entity.OrderListRequest;
import com.lenovo.mso2o.entity.OrderRectangle;
import com.lenovo.mso2o.rest.OrderAPI;
import com.lenovo.mso2o.rest.ServiceGenerator;
import com.lenovo.mso2o.ui.WebActivity;
import com.lenovo.mso2o.ui.adapter.OrderAdapter;
import com.lenovo.mso2o.ui.adapter.OrderViewHolder;
import com.lenovo.mso2o.util.Utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class POIMarkerFragment extends ActionBarFragment implements BaiduMap.OnMapStatusChangeListener, BaiduMap.OnMarkerClickListener, OrderAdapter.OnOrderSelectedListener{
    //控件绑定
    @Bind(R.id.layout_orderInfo)
    ListView orderInfoView;
    @Bind(R.id.mPoiMarkerMapView)
    MapView mMapView;
    @Bind(R.id.image_guideWay)
    ImageView image_mapMode;
    //全局变量
    private BaiduMap mBaidumap = null;//地图实例
    private MyLocationConfiguration.LocationMode mCurrentMode;//地图模式
    private BitmapDescriptor mCurrentMarker;//当前使用定位标注
    private BitmapDescriptor landMark;
    private BitmapDescriptor landMarkGray;
    private BitmapDescriptor mark_selected;
    private BitmapDescriptor mark_normal;
    private OrderAPI orderAPI;//数据接口
    private OrderListRequest orderListRequest;
    public  static int zoomLevel[] = {2000000,1000000,500000,200000,100000,
            50000,25000,20000,10000,5000,2000,1000,500,100,50,20,0};



    private List<Order> mOrders = new ArrayList<>();
            //获取到的订单列表
    private List<LatLng> locations;//位置集合
    private Subscription _subscription;

    private Set<MarkerOptions> mMarkeroptions;//标注数据集
    private Cluster mCluster;
    private Animation orderInfoAnim_in;
    private Animation orderInfoAnim_out;
    private int left;//屏幕左边界
    private int top;//屏幕上边界
    private int right;//屏幕右边界
    private int bottom;//屏幕下边界
    private int justonce = 1;

    private Point p1, p2, p3, p4;//屏幕四个脚标点
    private LatLng llg1, llg2, llg3, llg4;//与屏幕相对应的四个脚标地理坐标
    private boolean isMearured = false;


    //常量声明
    public static final int mMaxZoom = 12;
    public static double mDistance = 600000;
    //use average center by default
    public static boolean isAverageCenter = false;
    public static Integer mGridSize = 60;


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private Marker selected;
    private OrderRectangle rectangle1;
    private OrderRectangle rectangle2;
    private OrderRectangle rectangle3;
    private OrderRectangle rectangle4;
    private boolean inited = false;

    public POIMarkerFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderListRequest = new OrderListRequest();
        orderListRequest.setLimit(Integer.MAX_VALUE);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        rectangle1 = new OrderRectangle();
        rectangle2 = new OrderRectangle();
        rectangle3 = new OrderRectangle();
        rectangle4 = new OrderRectangle();
    }

    public void onResume(){
        super.onResume();
        mMapView.onResume();
        if (selected != null) {
            onMarkerClick(selected);
        }
    }

    /**
     * 获取屏幕宽高
     */
    public void getWidthAndHeight() {
        final DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowManager = getActivity().getWindowManager();
        windowManager.getDefaultDisplay().getMetrics(dm);
        mMapView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (!isMearured && mMapView != null) {
                    left = mMapView.getLeft();
                    top = dm.heightPixels - mMapView.getBottom();
                    right = mMapView.getRight();
                    bottom = mMapView.getBottom();
                    DLog.w("point", "left = " + left + " top = " + top + " right = " + right + " bottom = " + bottom);
                    p1 = new Point(left, top);
                    p2 = new Point(right, top);
                    p3 = new Point(right, bottom);
                    p4 = new Point(left, bottom);
                }
                //remove this!!
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    mMapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    mMapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        final int status = getArguments().getInt(MSConstants.FragmentType.class.getName(), 0);
        orderListRequest.setStatus(status);
        orderInfoView.setAdapter(new BaseListAdapter<Order>(new ArrayList<Order>()) {
            @Override
            protected View getNextView(final int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = View.inflate(getContext(), R.layout.order_item, null);
                }
                OrderAdapter.onBindOrder(new OrderViewHolder(convertView), getItem(position), status, Schedulers.newThread().createWorker(), orderAPI, POIMarkerFragment.this);
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Order order = getItem(position);
                        Intent intent = new Intent(ContextManager.context(), WebActivity.class);
                        intent.putExtra(WebActivity.ORDER, order);
                        intent.putExtra(FConstants.X5WEBVIEW_INITIAL_URL, String.format(MSConstants.APIConstants.ORDER_DETAIL_URL, order.getTicketid()));
                        DLog.d("order detail", String.format(MSConstants.APIConstants.ORDER_DETAIL_URL, order.getTicketid()));
                        int type = getArguments().getInt(MSConstants.FragmentType.FRAGMENT_ORDERTYPE);
                        startActivityForResult(intent, type);
                    }
                });
                return convertView;
            }

            @Override
            protected void onReachBottom() {
            }
        });
        //hardware accelerated
        /*
        if(mMapView != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                mMapView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            } else {
                mMapView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            }
        }
        */
        initData();
        getWidthAndHeight();
        return view;
    }

    /**
     * 初始化数据
     */
    public void initData() {
        mBaidumap = mMapView.getMap();
        mBaidumap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        mBaidumap.setMyLocationEnabled(true);
        //地图模式
        mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
        mCurrentMarker = null;
        //mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.main_icon_follow);
        //mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_mylocation);
        //landMark = BitmapDescriptorFactory.fromResource(R.drawable.icon_landmark);
        //landMarkGray = BitmapDescriptorFactory.fromResource(R.drawable.icon_landmarkgray);
        mark_normal = BitmapDescriptorFactory.fromResource(R.drawable.order_map_blue_sec);

        //mark_normal = BitmapDescriptorFactory.fromResource(R.drawable.back_bg);
        mark_selected = BitmapDescriptorFactory.fromResource(R.drawable.order_map_red_sec);
        mBaidumap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
        mBaidumap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                /*if(selected != null)
                    selected.setIcon(mark_normal);*/
                if (orderInfoView.getVisibility() == View.VISIBLE) {
                    orderInfoView.setVisibility(View.GONE);
                    orderInfoView.startAnimation(orderInfoAnim_out);
                }
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });
        //缩放地图状态监听
        mBaidumap.setOnMapStatusChangeListener(this);
        //标注点击监听
        mBaidumap.setOnMarkerClickListener(this);
        mBaidumap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                //30s update location
                Observable.create(new LocationEventsOnSubscribe(30*1000))
                        .compose(POIMarkerFragment.this.<BDLocation>bindToLifecycle())
                        .subscribe(new Action1<BDLocation>() {
                            @Override
                            public void call(BDLocation location) {
                                onReceiveLocation(location);
                            }
                        });
                mBaidumap.setOnMapLoadedCallback(null);
            }
        });

        locations = new ArrayList<>();
        mMarkeroptions = new HashSet<>();
        mCluster = new Cluster(getContext().getApplicationContext(), mMapView, isAverageCenter, mGridSize, mDistance);
        //数据请求相关初始化
        orderAPI = ServiceGenerator.createService(OrderAPI.class);
        //信息面板动画
        orderInfoAnim_in = AnimationUtils.loadAnimation(getContext().getApplicationContext(), R.anim.push_bottom_in);
        orderInfoAnim_out = AnimationUtils.loadAnimation(getContext().getApplicationContext(), R.anim.push_bottom_out);
    }

    @Override
    public int provideContentRes() {
        return R.layout.fragment_quick_order;
    }

    @Override
    protected void onFragmentInVisible(Bundle savedInstanceState) {

    }

    @Override
    protected void onFragmentVisible(Bundle savedInstanceState) {

    }

    public void onReceiveLocation(BDLocation bdLocation) {
        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(bdLocation.getRadius())
                        // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(100).latitude(bdLocation.getLatitude())
                .longitude(bdLocation.getLongitude()).build();
        // 设置定位数据
        mBaidumap.setMyLocationData(locData);
        //根据当前位置初始化请求参数
        getOrdersList(bdLocation);
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude()));

        LatLngBounds.Builder builder1 = new LatLngBounds.Builder();
        builder1.include(new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude()));
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLngBounds(builder1.build());
        mBaidumap.setMapStatus(u);

        onMapStatusChangeFinish(builder.build());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(_subscription != null && !_subscription.isUnsubscribed()){
            _subscription.unsubscribe();
        }
        //标注点击监听
        mBaidumap.setOnMapLoadedCallback(null);
        //缩放地图状态监听
        mBaidumap.setOnMapStatusChangeListener(null);
        //标注点击监听
        mBaidumap.setOnMarkerClickListener(null);
        if (mMapView != null) {
            mMapView.onDestroy();
            mMapView = null;
        }
        mCluster = null;
    }

    /**
     * 获取单据信息
     */
    public void getOrdersList(final BDLocation bdLocation) {
        if(_subscription == null || _subscription.isUnsubscribed())
        _subscription = orderAPI.orderList(orderListRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new NetworkObserver<Res<ArrayList<Order>>>() {
                    @Override
                    public void onSuccess(Res<ArrayList<Order>> arrayListResult) {
                        if (arrayListResult.getEcode() != 0) {
                            ErrorCode bizErrorCode = BizErrorCode.errorMap.get(arrayListResult.getEcode());
                            if(bizErrorCode != null) {
                                onFailure(bizErrorCode.getReason());
                            }else{
                                if(arrayListResult.getReason() != null && !TextUtils.isEmpty(arrayListResult.getReason()))
                                    onFailure(arrayListResult.getReason());
                                else
                                    onFailure(getString(R.string.unknown_error));
                            }
                            return;
                        }
                        if (arrayListResult.getResult() != null) {//数据成功获取
                            mOrders.clear();
                            mOrders.addAll(arrayListResult.getResult());
                            mMarkeroptions.clear();
                            for (Order order : mOrders) {
                                LatLng latlng = new LatLng(order.getLatitude(), order.getLongitude());
                                MarkerOptions option = new MarkerOptions()
                                        .position(latlng)
                                        .icon(mark_normal);

                                Bundle b = new Bundle();
                                b.putSerializable(Cluster.ITEM, order);
                                option.extraInfo(b);
                                mMarkeroptions.add(option);
                            }
                            refreshMarks();
                        } else {
                            //generateMarkers();
                            //Toast.makeText(getContext(), R.string.not_connect_to_server, Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(String tip) {
                        DLog.toast("fake success!" + tip);
                    }
                });

    }

    /**
     * 绘制订单标注方法
     */
    public void refreshOrderMarkers(List<MarkerOptions> optionses) {
        if (mBaidumap != null) {
            for (MarkerOptions option : optionses) {
                mBaidumap.addOverlay(option);
            }
        }
        if (mMapView != null)
            mMapView.refreshDrawableState();
    }

    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus) {
    }

    @Override
    public void onMapStatusChange(MapStatus mapStatus) {

    }

    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {
        Projection projection = mBaidumap.getProjection();
        llg1 = projection.fromScreenLocation(p1);
        llg2 = projection.fromScreenLocation(p2);
        llg3 = projection.fromScreenLocation(p3);
        llg4 = projection.fromScreenLocation(p4);

        if(!inited) {
            inited = true;
            /*
            float zoom = mBaidumap.getMapStatus().zoom;
            double meters = DistanceUtil.getDistance(llg1, llg2);
            for (int i = 0,zoomLen = SCALE.length; i < zoomLen; i++) {
                if(SCALE[i] - MSConstants.MIN_RADIS_AREA > 0){
                    zoom = 18-i+3;//之所以会多3，是因为地图范围常常是比例尺距离的10倍以上。所以级别会增加3。
                }
            };

            if (zoom < mBaidumap.getMaxZoomLevel()) {
                //float newZoom = (float) ((meters / MSConstants.MIN_RADIS_AREA) * mBaidumap.getMapStatus().zoom);
                DLog.w("area:", "zoom:new" + mBaidumap.getMapStatus().zoom + ":" + zoom + " radis:" +  meters + "");
                mBaidumap.setMapStatus(MapStatusUpdateFactory.zoomTo(zoom));
            }
            */

            //查表法得到zoomlevel
            int i;
            for(i=0;i<17;i++){
                if(zoomLevel[i]<= MSConstants.MIN_RADIS_AREA){
                    break;
                }
            }
            float zoom = i+8;
            MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(mapStatus.target, zoom);
            mBaidumap.setMapStatus(u);
        }

        DLog.i(getClass().getName(), "llg1=lat:" + llg1.latitude + "long=" + llg1.longitude +
                "---llg2=lat:" + llg2.latitude + "long=" + llg2.longitude +
                "---llg3=lat:" + llg3.latitude + "long=" + llg3.longitude +
                "---llg4=lat:" + llg4.latitude + "long=" + llg4.longitude);
        rectangle1.setLatitude(llg1.latitude);
        rectangle1.setLongitude(llg1.longitude);
        rectangle2.setLatitude(llg2.latitude);
        rectangle2.setLongitude(llg2.longitude);
        rectangle3.setLatitude(llg3.latitude);
        rectangle3.setLongitude(llg3.longitude);
        rectangle4.setLatitude(llg4.latitude);
        rectangle4.setLongitude(llg4.longitude);
        orderListRequest.getRectangle().clear();
        orderListRequest.getRectangle().add(rectangle1);
        orderListRequest.getRectangle().add(rectangle2);
        orderListRequest.getRectangle().add(rectangle3);
        orderListRequest.getRectangle().add(rectangle4);
        LatLng pp = mapStatus.target;
        BDLocation ll = new BDLocation();
        ll.setLatitude(pp.latitude);
        ll.setLongitude(pp.longitude);
        if(justonce == 1) {//仅更新一次数据
            justonce = -1;
            getOrdersList(new BDLocation(ll));
        }
    }

    //标注点击事件
    @Override
    public boolean onMarkerClick(Marker marker) {
        if(marker != selected && selected != null)
            selected.setIcon(mark_normal);
        BitmapDescriptor descriptor = marker.getIcon();
        List<Order> list = ((BaseListAdapter<Order>)orderInfoView.getAdapter()).getList();
        list.clear();
        if (//descriptor.hashCode() == landMark.hashCode() || descriptor.hashCode() == landMarkGray.hashCode() ||
                descriptor.hashCode() == mark_selected.hashCode() || descriptor.hashCode() == mark_normal.hashCode()) {

            if (orderInfoView.getVisibility() == View.VISIBLE) {
                orderInfoView.setVisibility(View.GONE);
                orderInfoView.startAnimation(orderInfoAnim_out);
                if(marker.getIcon().hashCode() == mark_selected.hashCode()){
                    marker.setIcon(mark_normal);
                }
            }
            Order order = (Order) marker.getExtraInfo().getSerializable(Cluster.ITEM);
            DLog.toast(order.toString());
            list.add(order);
            orderInfoView.setVisibility(View.VISIBLE);
            orderInfoView.startAnimation(orderInfoAnim_in);
            if(marker.getIcon().hashCode() == mark_normal.hashCode()){
                marker.setIcon(mark_selected);
            }
            selected = marker;
        } else {
            //TODO
            List<Order> list1 = (List<Order>) marker.getExtraInfo().getSerializable(Cluster.ITEMS);
            if(list1 != null)
               list.addAll(list1);
            if (mBaidumap.getMapStatus().zoom < mBaidumap.getMaxZoomLevel()) {
                float newZoom = mBaidumap.getMapStatus().zoom + 2.0f;
                mBaidumap.setMapStatus(MapStatusUpdateFactory.zoomTo(newZoom));
            }
            orderInfoView.setVisibility(View.VISIBLE);
            orderInfoView.startAnimation(orderInfoAnim_in);
        }

        ((BaseListAdapter<Order>) orderInfoView.getAdapter()).notifyDataSetChanged();
        ViewHelper.setListViewHeightBasedOnChildren(orderInfoView, 2);
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(marker.getPosition());
        MapStatusUpdate u = MapStatusUpdateFactory.newMapStatus(builder.build());
        mBaidumap.setMapStatus(u);
        return false;
    }

    @Override
    public int getActionBarRightResourceId() {
        return 0;
    }

    @Override
    public int getActionBarRight2ResourceId() {
        return 0;
    }

    @Override
    public void onActionBarRight2Click() {

    }

    @Override
    public void onActionBarRightClick() {

    }

    @Override
    public void onOrderSelected(Order order, Context context) {

    }

    @Override
    public void onConfirmClick() {

    }

    /**
     * 地图模式切换
     */
    @OnClick(R.id.image_guideWay)
    public void changeMapMode(View view) {
        if (mCurrentMode.equals(MyLocationConfiguration.LocationMode.NORMAL)) {
            image_mapMode.setImageResource(R.drawable.icon_guidenormal);
            mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING;
            mBaidumap
                    .setMyLocationConfigeration(new MyLocationConfiguration(
                            mCurrentMode, true, mCurrentMarker));

        } else if (mCurrentMode.equals(MyLocationConfiguration.LocationMode.FOLLOWING)) {
            image_mapMode.setImageResource(R.drawable.icon_guidefollow);
            mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
            mBaidumap
                    .setMyLocationConfigeration(new MyLocationConfiguration(
                            mCurrentMode, true, mCurrentMarker));

        }
    }

    /**
     * @param contentRelative
     * @param v
     */

    private void setImgViewParams(View contentRelative, double v) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        contentRelative.measure(w, h);
        int width = contentRelative.getMeasuredWidth();
        int height = contentRelative.getMeasuredHeight();
        ViewGroup.LayoutParams params = contentRelative.getLayoutParams();
        params.width = (int) (width * v);
        params.height = (int) (height * v);
        contentRelative.setLayoutParams(params);

    }

    /**
     * 如果当前地图缩放程度超过设置的最大值，则不改变点坐标，否则重新进行聚合运算
     */
    private void refreshMarks() {
        if (mBaidumap.getMapStatus().zoom >= mMaxZoom) {
            //refreshOrderMarkers(refreshVersionClusterMarker(mMarkeroptions));
            ArrayList<MarkerOptions> clusters = mCluster.createCluster(refreshVersionClusterMarker(mMarkeroptions));
            refreshOrderMarkers(clusters);
        } else {
            ArrayList<MarkerOptions> clusters = mCluster.createCluster(refreshVersionClusterMarker(mMarkeroptions));
            refreshOrderMarkers(clusters);
        }
    }

    public void onPause(){
        super.onPause();
        //mMapView.onPause();
    }

    private ArrayList<MarkerOptions> refreshVersionClusterMarker(Set<MarkerOptions> list) {
        MapStatus mapStatus = mBaidumap.getMapStatus();
        LatLngBounds latLngBounds = mapStatus.bound;

        ArrayList<MarkerOptions> result = new ArrayList<MarkerOptions>();
        result.addAll(list);
        return result;
    }

    @Override
    public boolean onBackPressed(){
        if (orderInfoView != null && orderInfoView.getVisibility() == View.VISIBLE) {
            orderInfoView.setVisibility(View.GONE);
            orderInfoView.startAnimation(orderInfoAnim_out);
            return true;
        }
        return false;
    }
}
